/*
 * @(#)HexGridExample.java	1.0 Apr 27, 2008
 *
 *	The MIT License
 *
 *	Copyright (c) 2008 Malachi de AElfweald <malachid@gmail.com>
 *
 *	Permission is hereby granted, free of charge, to any person obtaining a copy
 *	of this software and associated documentation files (the "Software"), to deal
 *	in the Software without restriction, including without limitation the rights
 *	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 *	copies of the Software, and to permit persons to whom the Software is
 *	furnished to do so, subject to the following conditions:
 *
 *	The above copyright notice and this permission notice shall be included in
 *	all copies or substantial portions of the Software.
 *
 *	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 *	THE SOFTWARE.
 */
package org.eoti.swing.hex;

import org.eoti.swing.tile.TileManager;

import javax.swing.*;
import java.awt.event.*;

public class HexGridExample
extends JFrame
implements HexGridListener
{
	public static void main(String[] args)
	{
		if(args.length != 3)
		{
			System.out.println("USAGE: java org.eoti.swing.HexGrid$Test cols rows size");
			System.out.println("EXAMPLE: java org.eoti.swing.HexGrid$Test 10 10 50");
			System.out.println("EXAMPLE: java org.eoti.swing.HexGrid$Test 10 10 3");
			System.exit(0);
		}

		try{
			int cols = Integer.parseInt(args[0]);
			int rows = Integer.parseInt(args[1]);
			int size = Integer.parseInt(args[2]);

			JFrame.setDefaultLookAndFeelDecorated(true);
			HexGridExample example = new HexGridExample(cols, rows, size);
		}catch(Exception e){
			System.out.format("Exception: %s", e.getMessage());
			e.printStackTrace();
		}
	}

	public HexGridExample(int cols, int rows, int size)
	throws Exception
	{
		super("HexGrid$Test");
		System.out.println("Quick Warning... It appears that loading the LARGE tiles is slow. fix that.");
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		TileManager<String> tileMgr = new TileManager<String>();
		tileMgr.addResource("jungle", "/org/eoti/gfx/examples/jungle.jpg");
		tileMgr.addResource("volcano", "/org/eoti/gfx/examples/volcano.jpg");
//			tileMgr.add("jungle", new File("jungle.jpg"));
//			tileMgr.add("volcano", new File("volcano.jpg"));

		HexGrid grid = new HexGrid(cols, rows, size);
		grid.setTileManager(tileMgr);
		grid.addHexGridListener((HexGridListener)this);
		grid.setBorderPainted(true);
		grid.setTextOn(size > 30);
		this.getContentPane().add(new JScrollPane(grid));
//			this.pack();
//			this.setSize(grid.getPreferredSize());
//Dimension d = grid.getPreferredSize();
//System.out.println("Preferred Size: " + d.getWidth() + "x" + d.getHeight());
		this.setSize(700, 500);

		grid.setTileName(2,2, "volcano");
		grid.setTileName(2,4, "jungle");
		grid.setTileName(4,3, "jungle");
		grid.setTileName(6,6, "volcano");
		this.setVisible(true);
	}

	public void hexChosen(HexGridEvent event)
	{
		if(event.getInstigator().getID() == MouseEvent.MOUSE_CLICKED)
		{
			String tileName = event.getTileName();
			if(tileName == null)
				System.out.println("Empty Tile Clicked: " + event.getPosition());
			else
				System.out.println(tileName + " Clicked: " + event.getPosition());
		}
	}
}
